package Demo1;
import java.sql.*;

//处理相关数据库的操作
public class DbUtil {
    private static String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
    private static String user="root";
    private static String password="123456";
    private static Connection conn=null;
    private static PreparedStatement ps=null;
    private static ResultSet rs=null;

    public static void load(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            System.out.println("数据库连接异常"+e.getMessage());
            //e.printStackTrace();
        }
    }

    //查找学生总人数
    public int studentCount(){
        String sql ="select count(1) from s_student";
        try {
            ps =conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                int anInt = rs.getInt(1);
                return anInt;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    //增加学生
    public boolean studentAdd(String sname, String sex, String phone, String  qq, String wechat, int gid, String  enterdate, String fromcity, String birthday, String  address, String  pass,String idcardpic){
        String sql ="INSERT INTO `s_student` ( `sname`, `sex`, `telephone`, `qq`, `wechat`, `gradeid`, `enterdate`, `fromcity`, `birthday`, `address`, `pass`, `idcardpic`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,sname);
            ps.setString(2,sex);
            ps.setString(3,phone);
            ps.setString(4,qq);
            ps.setString(5,wechat);
            ps.setInt(6,gid);
            ps.setDate(7, Date.valueOf(enterdate));
            ps.setString(8,fromcity);
            ps.setDate(9,Date.valueOf(birthday));
            ps.setString(10,address);
            ps.setString(11,pass);
            ps.setString(12,idcardpic);
            ps.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //删除学生
    public boolean studentDel(int stuno){
        String sql ="delete from s_student where stuno=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,stuno);
            ps.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //统计各个性别人数
    public void sexCount(){
        String sql ="select sex,count(1) as 'renshu' from s_student GROUP BY sex";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String sex = rs.getString("sex");
                int renshu = rs.getInt("renshu");
                System.out.println(sex+" "+renshu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
